package com.gxa.common.utils;

import com.gxa.common.page.AccommodationQueryPage;
import com.gxa.common.page.QueryPage;

public class PageUtils {

    public static final int DEFAULT_PAGE_SIZE = 10;
    public static final int DEFAULT_CURRENT_PAGE = 1;
    public static final int MAX_PAGE_SIZE = 30;

    public static <T extends QueryPage> void validate(T t) {
        if (t != null) {
            if (t.getPageSize() != null && t.getCurrentPage() != null) {
                t.setCurrentPage(t.getCurrentPage() < 1 ? DEFAULT_CURRENT_PAGE : t.getCurrentPage());
                t.setPageSize((t.getPageSize() < DEFAULT_PAGE_SIZE && t.getPageSize() < MAX_PAGE_SIZE) ? DEFAULT_PAGE_SIZE : t.getPageSize());
            } else {
                t.setCurrentPage(DEFAULT_CURRENT_PAGE);
                t.setPageSize(DEFAULT_PAGE_SIZE);
            }
        } else {
            throw new RuntimeException("queryPage is null");
        }
    }

    public static Integer getStart(Integer currentPage, Integer pageSize) {
        return (currentPage - 1) * pageSize;
    }

    public static <T extends QueryPage> Integer getStart(T t) {
        validate(t);
        return (t.getCurrentPage() - 1) * t.getPageSize();
    }
}
